package cn.com.callback

import groovy.json.*;
import java.util.Map;

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import java.text.ParseException
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import cn.com.elite.EliteBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;

import cn.com.elite.Util;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

/*
 * @purpose: 创蓝短信发送接口
 * @示例报文：
 * */

/**
 *
 * @Title: BP_CLjob_SmsSend
 * @Description: 宝安排水创蓝短信发送接口对接
 * @author: chieh
 * @date 2021-03-12 12:28:00
 *
 */
public class BP_CLjob_SmsSend implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(BP_CLjob_SmsSend.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String paramString){

		log.debug("----------------------------------start BP_CLjob_SmsSend from 开始发送短信 -----------------------------");//日志标识，方便查找
		//判断是否有需要发送的短信
		def getSmsSelectSQL = eliteBean.chaxun_sql("select DOCUMENTOUT_GUID,DOCUMENTTO,DOCUMENTPATH as DOCUMENTPATH,REPLACE(REPLACE(REPLACE(substring( convert(varchar,Expectedstartdate,120),1,16),'-',''),' ',''),':','') as Expectedstartdate,model_1 as msg_1,model_2 as msg_2,model_3 as msg_3,model_4 as msg_4,model_5 as msg_5 from documentout where ExpectedStartDate<=GETDATE() and STATUS=0 and response_msg is null","dataSource");
		log.debug("SQL获取数据:"+getSmsSelectSQL);
		if(getSmsSelectSQL.size() > 0) {
			log.debug("BP_CLjob_SmsSend开始处理短信发送任务--------------------------");
			//获取配置信息
			//def url = getProperty.get("SMS.URL");
			def url = "http://smssh1.253.com/msg/variable/json";
			def account = getProperty.get("SMS.ACCOUNT");
			def password = getProperty.get("SMS.PASSWORD");
			log.debug("url:" + url+"	,account:" + account+"	,password:" + password);

			String documentOutGUID="";//documentout表，主键，用于更新数据
			String DOCUMENTTO="";//手机号码
			String DOCUMENTPATH ="";//短信内容
			String msg_1 =""; //内容参数1
			String msg_2 =""; //内容参数2
			String msg_3 =""; //内容参数3
			String msg_4 =""; //内容参数4
			String msg_5 =""; //内容参数5
			
			String params="";//拼接内容 手机号码和变量参数，多组参数使用英文分号;区分，必填
			String sendtime="";//定时发送短信时间 默认立即发送；且定时时间在7天以内
			String report=false;//是否需要状态报告（默认false）
			String extend="555";//下发短信号码扩展码
			String uid="";//该条短信在您业务系统内的ID 用于区分短信业务，总位数不超过40位
			
			String returnDealFlag="";//flag状态1成功 0失败
			String msgId="";
			String time="";
			String errorMsg="";//返回报文
			
			try{
				for(int i=0;i<getSmsSelectSQL.size();i++) {
					documentOutGUID = getSmsSelectSQL.get(i).get("DOCUMENTOUT_GUID");
					DOCUMENTTO= getSmsSelectSQL.get(i).get("DOCUMENTTO");
					DOCUMENTPATH= getSmsSelectSQL.get(i).get("DOCUMENTPATH");
					sendtime= getSmsSelectSQL.get(i).get("Expectedstartdate");
					msg_1 =getSmsSelectSQL.get(i).get("msg_1");
					msg_2 =getSmsSelectSQL.get(i).get("msg_2");
					msg_3 =getSmsSelectSQL.get(i).get("msg_3");
					msg_4 =getSmsSelectSQL.get(i).get("msg_4");
					msg_5 =getSmsSelectSQL.get(i).get("msg_5");
					
					if(msg_1==null) {
						params=DOCUMENTTO;
					}else if(msg_1!=null) {
						params=DOCUMENTTO+","+msg_1;
						if(msg_2!=null) {
							params=params+","+msg_2;
							if(msg_3!=null) {
								params=params+","+msg_3;
								if(msg_4!=null) {
									params=params+","+msg_4;
									if(msg_5!=null) {
										params=params+","+msg_5;
									}
								}
								
							}
							
						}
						
					}
					log.debug("params:" + params);
					
					uid = UUID.randomUUID().toString();
					
					String json="{\"account\":\""+account+"\",\"password\":\""+password+"\",\"msg\":\""+DOCUMENTPATH+"\",\"params\":\""+params+"\",\"sendtime\":\""+sendtime+"\",\"report\":\""+report+"\",\"extend\":\""+extend+"\",\"uid\":\""+uid+"\"}";
					log.debug("获取接口入参:	"+json);

					// 设置头文件
					Map<String, String> headMap = new HashMap<String, String>();
					headMap.put("Content-Type", "application/json");
					String injson = Util.doHttpPostUseHead(url, json, "UTF-8", 100000, 0, headMap);

					log.debug("调用返回信息：" + injson.toString());
					JSONObject inObject = new JSONObject(injson);
					String code = KHUtil.GetJV(inObject, "code");
					//msgId = KHUtil.GetJV(inObject, "msgId");
					//time = KHUtil.GetJV(inObject, "time");
					errorMsg = KHUtil.GetJV(inObject, "errorMsg");
					
					if(code=='0') {
						returnDealFlag=1;
					}else {
						returnDealFlag=0;
					}
					eliteBean.update("update DocumentOut set request_msg = '"+params+"',response_msg='"+errorMsg+"',Status = '"+returnDealFlag+"',LastTryDateTime=GETDATE(),FinishDateTime=GETDATE(),ModifiedDate=GETDATE()  where DocumentOut_GUID = '"+documentOutGUID+"'", "dataSource");
				}

			} catch(Exception) {
				log.error("异常",Exception); //打印日志
			}
			log.debug("returnMessage:" + errorMsg+"	Status:" + returnDealFlag+"	documentOutGUID:" + documentOutGUID);
			log.debug("BP_CLjob_SmsSend当前短信发送成功--------------------------");
		}
		log.debug("BP_CLjob_SmsSend当前无短信发送任务--------------------------");

	}

}
